package org.example.lottery_system.common.pojo;

import lombok.Data;
import org.example.lottery_system.common.errorcode.ErrorCode;
import org.example.lottery_system.common.errorcode.GlobalErrrorCodeConstants;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
//Seril..是因为要序列化,因为这个是要在json中的?
public class CommonResult<T> implements Serializable {
    /**
     * 返回的错误码
     */
    private Integer code;
    /**
     * 返回的数据
     */
    private T data;
    /**
     * 返回的描述
     */
    private String msg;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrrorCodeConstants.SUCCESS.getCode().equals(code),"code不是错误代码");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg=  msg;
        return result;
    }
    public static <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMsg());
    }
}
